package com.itheima.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itheima.entity.AccountMessage;
import com.itheima.mapper.AccountMessageMapper;
import com.itheima.pojo.jo.AccountMassagePo;
import com.itheima.service.IAccountMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户消息Service业务层处理
 * 
 * @author guanhuaer
 * @date 2025-03-19
 */
@Service
public class AccountMessageServiceImpl implements IAccountMessageService
{
    @Autowired
    private AccountMessageMapper accountMessageMapper;

    /**
     * 查询用户消息
     * 
     * @param id 用户消息主键
     * @return 用户消息
     */
    @Override
    public AccountMessage selectAccountMessageById(Long id)
    {
        return accountMessageMapper.selectAccountMessageById(id);
    }

    /**
     * 查询用户消息列表
     * 
     * @param accountMessage 用户消息
     * @return 用户消息
     */
    @Override
    public List<AccountMessage> selectAccountMessageList(AccountMessage accountMessage)
    {
        return accountMessageMapper.selectAccountMessageList(accountMessage);
    }

    /**
     * 新增用户消息
     * 
     * @param accountMessage 用户消息
     * @return 结果
     */
    @Override
    public int insertAccountMessage(AccountMessage accountMessage)
    {
        return accountMessageMapper.insertAccountMessage(accountMessage);
    }

    /**
     * 修改用户消息
     * 
     * @param accountMessage 用户消息
     * @return 结果
     */
    @Override
    public int updateAccountMessage(AccountMessage accountMessage)
    {
        return accountMessageMapper.updateAccountMessage(accountMessage);
    }

    /**
     * 批量删除用户消息
     * 
     * @param ids 需要删除的用户消息主键
     * @return 结果
     */
    @Override
    public int deleteAccountMessageByIds(Long[] ids)
    {
        return accountMessageMapper.deleteAccountMessageByIds(ids);
    }

    /**
     * 删除用户消息信息
     * 
     * @param id 用户消息主键
     * @return 结果
     */
    @Override
    public int deleteAccountMessageById(Long id)
    {
        return accountMessageMapper.deleteAccountMessageById(id);
    }

    @Override
    public Map<String, Object> selectAccountMessageByUserId(Long id) {
        List<AccountMessage> accountMessages = accountMessageMapper.selectAccountMessageByUserId(id);
        List<Object> notifyData = new ArrayList<>();
        List<Object> messageData = new ArrayList<>();
        List<Object> todoData = new ArrayList<>();
        Integer notifyCount = 0;
        Integer messageCount = 0;
        Integer todoCount = 0;
        for (AccountMessage accountMessage : accountMessages) {
            if (accountMessage.getType() == 0){
                notifyData.add(accountMessage);
                if (accountMessage.getIsRead() == 0){
                    notifyCount++;
                }
            }else if (accountMessage.getType() == 1){
                messageData.add(accountMessage);
                if (accountMessage.getIsRead() == 0){
                    messageCount++;
                }
            } else if (accountMessage.getType() == 2){
                if (accountMessage.getIsRead() == 0){
                    todoCount++;
                }
                todoData.add(accountMessage);
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("notifyData", notifyData);
        result.put("messageData", messageData);
        result.put("todoData", todoData);
        result.put("notifyCount", notifyCount);
        result.put("messageCount", messageCount);
        result.put("todoCount", todoCount);
        return result;
    }

    /**
     * 根据用户id和消息类型修改信息的是否已读状态
     * @param massagePo
     */
    @Override
    public void updateMessageReadStatus(AccountMassagePo massagePo) {
        accountMessageMapper.updateMessageReadStatus(massagePo);
    }

    /**
     * 查询消息历史记录
     * @param massagePo
     * @return
     */
    @Override
    public List<AccountMessage> selectMassageHistory(AccountMassagePo massagePo) {
        return accountMessageMapper.selectMassageHistory(massagePo);
    }
}
